package com.morizhang.merchanmanager.api.plugins.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.morizhang.merchanmanager.api.bean.MerChanManagerException;
import com.morizhang.merchanmanager.api.bean.StatisticDataHolder;
import com.morizhang.merchanmanager.api.dao.CommodityStorageDao;
import com.morizhang.merchanmanager.api.dao.PriceInfoDao;
import com.morizhang.merchanmanager.api.model.PriceModel;
import com.morizhang.merchanmanager.api.model.StockModel;
import com.morizhang.merchanmanager.api.plugins.BasicStockManage;
import com.morizhang.merchanmanager.api.plugins.PluginAnnotation;
import com.morizhang.merchanmanager.api.utils.SerialNoGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@PluginAnnotation(name = "BasicStockManage")
public class BasicStockManageImpl implements BasicStockManage {
    @Autowired
    Environment environment;
    @Autowired
    CommodityStorageDao commodityStorageDao;
    @Autowired
    PriceInfoDao priceInfoDao;
    @Override
    public void doStock(JSONArray stockList) throws Exception {
        try {
            if (stockList == null || stockList.isEmpty())
                return;
            List<StockModel> stockListFormed = new ArrayList<>();
            for (Iterator i = stockList.stream().iterator(); i.hasNext(); ) {
                JSONObject singleStockRecord = (JSONObject) i.next();
                StockModel singleStockModel = JSON.toJavaObject(singleStockRecord, StockModel.class);
                String serialNo = SerialNoGenerator.newSerialNo();
                singleStockModel.setSerialNo(serialNo);
                stockListFormed.add(singleStockModel);
            }

            for (StockModel singleStockModel : stockListFormed) {
                //判断将要入库的项目是否与已存在的货物在生产日期上冲突
                StockModel existCheck = commodityStorageDao.queryCommodityStorage(singleStockModel);
                if (existCheck != null && existCheck.getProductTime().compareTo(singleStockModel.getProductTime()) != 0) {
                    MerChanManagerException exception = new MerChanManagerException();
                    throw exception;
                }
                //判断将要入库的项目是否与已存在的货物在价格上冲突
                PriceModel priceModel = new PriceModel(singleStockModel);
                PriceModel existPrice = priceInfoDao.queryCommodityStockPriceByBarcodeBatchNo(priceModel);
                if (existPrice != null && existPrice.getStockPrice() != singleStockModel.getStockAmount()) {
                    MerChanManagerException exception = new MerChanManagerException();
                    throw exception;
                }
                //如存在一致生产日期的项目则直接增加数量
                if (existCheck != null && existCheck.getProductTime().compareTo(singleStockModel.getProductTime()) == 0) {
                    existCheck.setAmountNow(singleStockModel.getStockAmount() + existCheck.getAmountNow());
                    doUpdateStockDao(existCheck);
                    continue;
                }
                int amountNow = singleStockModel.getStockAmount();
                singleStockModel.setAmountNow(amountNow);
                doStockDao(singleStockModel);

                StatisticDataHolder.makeExpire("storageValue");
                StatisticDataHolder.makeExpire("storageSumNum");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public void undoStock() {

    }

    @Transactional
    protected void doStockDao(StockModel stockModel) throws Exception {
        try {
            commodityStorageDao.addCommodityStorage(stockModel);
            StockModel unlocationExist = commodityStorageDao.queryCommodityUnlocation(stockModel);
            if (unlocationExist == null) {
                commodityStorageDao.addCommodityUnlocation(stockModel);
            } else {
                unlocationExist.setAmountNow(unlocationExist.getStockAmount() + stockModel.getAmountNow());
                commodityStorageDao.updateCommodityUnlocation(unlocationExist);
            }
            PriceModel stockPrice = new PriceModel(stockModel);
            stockPrice.setSerialNo(SerialNoGenerator.newSerialNo());
            priceInfoDao.addCommodityStockPrice(stockPrice);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Transactional
    protected void doUpdateStockDao(StockModel stockModel) {
        try {
            commodityStorageDao.updateCommodityStorage(stockModel);
            StockModel unlocationExist = commodityStorageDao.queryCommodityUnlocation(stockModel);
            if (unlocationExist == null) {
                commodityStorageDao.addCommodityUnlocation(stockModel);
            } else {
                unlocationExist.setAmountNow(unlocationExist.getStockAmount() + stockModel.getAmountNow());
                commodityStorageDao.updateCommodityUnlocation(unlocationExist);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

}
